package com.jwdf.uda.ui.fragment.search;

import android.os.Bundle;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.SearchAction;
import com.jwdf.uda.ui.adapter.GenericQuizAdapter;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;

import java.util.List;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/9/19.
 */
public class SearchQuestionFragment extends SimpleListFragment {

    protected PbBean.PbSignSearchType restType= PbBean.PbSignSearchType.SEARCH_TYPE_QUESTION;
    private GenericQuizAdapter mAdapter;
    private String mContent;


    public static SearchQuestionFragment instance(String content) {
        SearchQuestionFragment view = new SearchQuestionFragment();
        Bundle args = new Bundle();
        args.putString("content", content);
        view.setArguments(args);
        return view;
    }

    @Override
    protected void initViews() {
        mAdapter = new GenericQuizAdapter(getActivity());
        lv.setAdapter(mAdapter);
    }
    @Override
    protected void onListItemClick(int position) {

    }
    @Override
    protected void requestData(final boolean refresh) {

        if(refresh && mAdapter.isEmpty()) {
            showProgressDialog();
        }

        int offset = refresh ? 0 : mAdapter.getCount();
        onNewRequestCall(SearchAction.newInstance(getContext(), mContent, offset,restType).request(new AHttpService.IResCallback<PbBean.PbResPageSearch>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResPageSearch> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
                    closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        mDataBinding.commonFragmentList.stopRefresh();
                        mDataBinding.commonFragmentList.stopLoadMore();
                        return;
                    }
                    doSuccessResponse(refresh, response.body());
                }
            }
        }));

    }
    private void doSuccessResponse(boolean refresh, PbBean.PbResPageSearch body) {
        if(body != null) {
            final List<PbBean.PbBaseQuestion> questionInfoList = body.getQuestionListList();
            if(refresh) {
                mAdapter.setData(questionInfoList);
            } else {
                mAdapter.append(questionInfoList);
            }
            if(questionInfoList.size() == 0) {
                mDataBinding.commonFragmentList.setEmptyView(mDataBinding.tvSearchEmptyData);
            }
            mDataBinding.commonFragmentList.setPullLoadEnable(questionInfoList.size() == getPageLength());
            mDataBinding.commonFragmentList.stopRefresh();
            mDataBinding.commonFragmentList.stopLoadMore();
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContent = getArguments().getString("content", "");
    }
}
